package com.miya.demo.service.impl;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.convert.Convert;
import com.miya.demo.entity.Menu;
import com.miya.demo.entity.Role;
import com.miya.demo.mapper.MenuMapper;
import com.miya.demo.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色和impl permisson服务
 *
 * @author CaiXiaowei
 * @date 2022/07/11
 */
@Service
public class RoleAndPermissonServiceImpl implements StpInterface {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<Menu> menus = menuMapper.getByUserId(Convert.toLong(loginId));
        return menus.stream().map(Menu::getPerms).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<Role> roles = roleMapper.getByUserId(Convert.toLong(loginId));
        return roles.stream().map(Role::getRoleName).collect(Collectors.toList());
    }
}
